package com.boxin.ims.modules.wechat.busi;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.boxin.ims.modules.wechat.entity.TextMessage;
import com.boxin.ims.modules.wechat.entity.UserDefine;
import com.boxin.ims.modules.wechat.entity.WechatMember;
import com.boxin.ims.modules.wechat.score.entity.ExchangeRecord;
import com.boxin.ims.modules.wechat.score.entity.ScoreExchange;
import com.boxin.ims.modules.wechat.score.service.ScoreExchangeService;
import com.boxin.ims.modules.wechat.service.UserDefineService;
import com.boxin.ims.modules.wechat.utils.ScoreUtils;
import com.thinkgem.jeesite.common.utils.DateUtils;

/**
 * @author Jakemanse
 * @date 2013-10-3
 * @function <p>积分兑换处理 </p>
 */
@Service
public class ExchangeProcessor extends BusinessProcessor{

	@Autowired
	private ScoreExchangeService scoreExchangeService;
	
	@Autowired
	private UserDefineService userDefineService;
	
	public ExchangeProcessor(){
		super();
	}
	
	
	
	@Override
	public ProcessMessage process() {
		
		
		String reply = "";
		TextMessage txtMsg = new TextMessage(messageMap);
		
		WechatMember member =	memberCheckAndReply();	//检查会员
		
		if(member !=null){
			
			//该服务是否停止
			UserDefine userDefine = userDefineService.getByUserId(user.getId());	//定义
			
			if(userDefine != null && UserDefine.NO.equals(userDefine.getExchangeSwitch())){	//签到服务停止
				reply =  "积分兑换活动已停止,启动时间请等通知,谢谢!" ;
				if(StringUtils.isNotEmpty(userDefine.getExchangeSwitchDesc())){
					reply = userDefine.getExchangeSwitchDesc();
				}
				writeToUser(reply);
				return null;
			}
			
			
			
			String upcon = txtMsg.getContent();
			upcon = upcon.replaceAll("兑换", "");
			if( !StringUtils.isEmpty(upcon)){
				String key = upcon;
				ScoreExchange exchange = scoreExchangeService.getByMessage(user.getId(), key); 
				if(exchange == null){
					reply ="亲，对不起，未能找到对应的奖品.请回复'积分兑换'查询奖品,谢谢！";
				}else{
					int cscore = exchange.getScore();
					int myscore = member.getActiveScore();
					if(myscore < cscore){
						reply ="亲，对不起，您的积分不足,不能兑换积分为"+exchange.getScore()/1000 +"的"+exchange.getName()+".";
					}else{
						member.setActiveScore(myscore - cscore);
						member.setScore(member.getScore()-cscore);
						wechatMemberService.save(member);
						ExchangeRecord record =	ScoreUtils.addWechatMemberConsumeScore(member,exchange);
						
						String validDate = DateUtils.formatDate((record.getEndTime()==null)?new Date():record.getEndTime(), "MM月dd日");
						reply = "亲，恭喜您已成功兑换"+exchange.getName()+"["+exchange.getScore()/1000+"积分].您当前剩余的积分为:" + member.getActiveScore()/1000+",请务删除此信息，于"+validDate+"之前到店里凭此二维码核销使用,过期作废.谢谢!";
						//兑换成功后回复二维码图片
						String title = "积分兑换成功";
						String desc = reply;
						if(userDefine!=null){
							
							if(StringUtils.isNotEmpty(userDefine.getExchangeTitle())){
								title = userDefine.getExchangeTitle();
							}
							
							if(StringUtils.isNotEmpty(userDefine.getExchangeDesc())){
								desc = userDefine.getExchangeDesc();
								desc = desc.replaceAll("%奖品%", exchange.getName());
								desc = desc.replaceAll("%消费积分%", exchange.getScore()/1000+"");
								desc = desc.replaceAll("%有效期%", validDate);
								desc = desc.replaceAll("%积分%", member.getScore()/1000+"");
							}
						}
						
						writeImageToUser(title, desc,record.getQrCode().getId() );
						return null;
						
					}
				}
				
			}else{
				reply ="亲,对不起，您输入的不正确,未能找到对应的奖品.请回复'积分兑换'查询奖品,谢谢！.";                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
			}
			writeToUser(reply);	//回复信息
		} 
		
		
		return null;
	}

	@Override
	public boolean isNeedBeMember() {
		return true;
	}
	
	

}
